package videoCourse.Structure;

import javax.swing.tree.TreeNode;

public class BinaryNode {

    public int value;
    private BinaryNode leftNode;
    private BinaryNode rightNode;

    public BinaryNode(int value){
        this.value = value;
    }

    //public int getValue(){
//        return  this.value;
//    }




    public void setLeftNode(BinaryNode leftNode) {
        this.leftNode = leftNode;
    }

    public void setRightNode(BinaryNode rightNode) {
        this.rightNode = rightNode;
    }

    public void frontShow() {

        System.out.println(value);
        if(leftNode != null){
            leftNode.frontShow();
        }
        if(rightNode != null){
            rightNode.frontShow();
        }
    }

    public void midShow() {

        if(leftNode != null){
            leftNode.midShow();
        }
        System.out.println(value);
        if(rightNode != null){
            rightNode.midShow();
        }
    }

    public void endShow() {

        if(leftNode != null){
            leftNode.endShow();
        }
        if(rightNode != null){
            rightNode.endShow();
        }
        System.out.println(value);
    }

    public BinaryNode frontSerch(int i) {
        BinaryNode target = null;
        if(this.value == i) return  this;
        else{
            if(leftNode!=null){
                target = leftNode.frontSerch(i);
            }
            if(target != null ){
                return target;
            }
            if(rightNode != null){
                target = rightNode.frontSerch(i);
            }
        }
        return target;
    }

    public void deleteTree(int i) {
        BinaryNode parent = this;
        if(this.leftNode != null && this.leftNode.value == i){
            this.setLeftNode(null);
            return;
        }else if(this.rightNode != null && this.rightNode.value == i){
            this.setRightNode(null);
            return;
        }
        if(this.leftNode != null)
        leftNode.deleteTree(i);
        if(this.rightNode != null)
        rightNode.deleteTree(i);
    }
}
